package chujisuanfa;

public class LongestCommonPrefix {
    public static String longestCommonPrefix(String[] strs){
        int slen =strs.length;
        int tlen=0; //前缀长度
        if(strs[0].length()==0) return "";
        char c;
        StringBuilder target = new StringBuilder();
        while(tlen<strs[0].length()){
            c= strs[0].charAt(tlen);
            for (int i=1;i<slen;++i){
                if (tlen>=strs[i].length()||strs[i].charAt(tlen)!=c) return target.toString();
            }
            target.append(strs[0].charAt(tlen));
            tlen++;
        }

        return target.toString();
    }

    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"c","a","abjrg"}));
    }
}
